import java.util.LinkedList;
import java.util.Stack;

public class Test {


    //https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack =new Stack<>();
        int j=0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.isEmpty() && j< popV.length && stack.peek()==popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }


    //https://leetcode.cn/problems/evaluate-reverse-polish-notation/
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack =new Stack<>();
        for(String str : tokens){
            if (!isOperator(str)){
                int x=Integer.parseInt((str));
                stack.push(x);
            }else {
                int val2=stack.pop();
                int val1=stack.pop();
                switch (str){
                    case "+":
                        stack.push(val1+val2);
                        break;
                    case "-":
                        stack.push(val1-val2);
                        break;
                    case "*":
                        stack.push(val1*val2);
                        break;
                    case "/":
                        stack.push(val1/val2);
                        break;
                }
            }
        }
        return stack.pop();

    }

    private boolean isOperator(String ch){
        if(ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/")){
            return true;
        }
        return false;
    }

    //https://leetcode.cn/problems/valid-parentheses/
    public boolean isValid(String s){
        Stack<Character> stack =new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if(ch=='(' || ch=='{' || ch=='['){
                stack.push(ch);
            }else{
                if (stack.isEmpty()){
                    return false;
                }
                char ch2=stack.peek();
                if(ch == ')' && ch2 == '('
                        || ch == '}' && ch2 == '{'
                        || ch == ']' && ch2 == '['){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if (!stack.isEmpty()){
            return false;
        }
        return true;
    }

    public static void main3(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();

        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);

        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }


    public static void main2(String[] args) {
        MyStack stack=new MyStack();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        System.out.println(stack.pop());
        System.out.println(stack.pop());

    }

    public static void main1(String[] args) {
        Stack<Integer> stack=new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);

        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }
}
